package com.dotflat.rest.controller;

import cn.hutool.core.util.StrUtil;
import com.dotflat.rest.utils.WXUtils;
import com.starinside.wp.foundation.model.StatusResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/wx/api")
public class WxConfigController {
    @Autowired
    private WXUtils wxUtils;
    /**
     * 获取jsapi所需参数
     * @param webUrl
     * @return
     */
    @RequestMapping(value = "/getWxConfigParams", method = RequestMethod.GET)
    public StatusResponse getWxConfigParams(@RequestParam("webUrl")String webUrl){
        Map<String, String> params = wxUtils.wxConfigParams(webUrl);
        return StatusResponse.success(params);
    }

    /**
     * 返回token
     */
    @GetMapping(produces ="text/plain;charset=utf-8")
    public String backToken(
            @RequestParam(value = "signature", required = false) String signature,
            @RequestParam(value = "timestamp", required = false) String timestamp,
            @RequestParam(value = "nonce", required = false) String nonce,
            @RequestParam(value = "echostr", required = false) String echostr) {

        try {
            //首先先进行判断参数是否为null
            log.info("微信公众号验签信息{}开始 [{}, {}, {}, {}]", signature, timestamp, nonce, echostr);
            if (StrUtil.isAllBlank(signature, timestamp, nonce, echostr)) {
                log.error("微信验证签名参数非法为空");
            }
            System.out.println(echostr);
            return echostr;
        } catch (Exception e) {
            log.error("微信验证签名异常{} [{}{}{}{}]", signature, timestamp, nonce, echostr);
            return null;
        }
    }
}